package com.example.a_java.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a_java.entity.RepaymentPlan;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 还款计划Mapper接口
 */
@Mapper
public interface RepaymentPlanMapper extends BaseMapper<RepaymentPlan> {

    /**
     * 查询还款计划分页列表（支持多条件查询）
     *
     * @param page          分页参数
     * @param contractNo    合同编号
     * @param loanNo        贷款编号
     * @param customerName  客户姓名
     * @param repayStatus   还款状态
     * @param repayMethod   还款方式
     * @param startDate     开始日期
     * @param endDate       结束日期
     * @return 还款计划分页数据
     */
    @Select("<script>SELECT * FROM repayment_plan WHERE 1=1 " +
            "<if test='contractNo != null and contractNo != \"\"'> AND contract_no LIKE CONCAT('%', #{contractNo}, '%') </if>" +
            "<if test='loanNo != null and loanNo != \"\"'> AND loan_no LIKE CONCAT('%', #{loanNo}, '%') </if>" +
            "<if test='customerName != null and customerName != \"\"'> AND customer_name LIKE CONCAT('%', #{customerName}, '%') </if>" +
            "<if test='repayStatus != null'> AND status = #{repayStatus} </if>" +
            "<if test='repayMethod != null and repayMethod != \"\"'> AND repayment_method = #{repayMethod} </if>" +
            "<if test='startDate != null'> AND start_date &gt;= #{startDate} </if>" +
            "<if test='endDate != null'> AND end_date &lt;= #{endDate} </if>" +
            " ORDER BY id DESC</script>")
    Page<RepaymentPlan> selectPlanPage(Page<RepaymentPlan> page,
                                       @Param("contractNo") String contractNo,
                                       @Param("loanNo") String loanNo,
                                       @Param("customerName") String customerName,
                                       @Param("repayStatus") Integer repayStatus,
                                       @Param("repayMethod") String repayMethod,
                                       @Param("startDate") String startDate,
                                       @Param("endDate") String endDate);

    /**
     * 根据贷款编号查询还款计划
     * 
     * @param loanNo 贷款编号
     * @return 还款计划
     */
    @Select("SELECT * FROM repayment_plan WHERE loan_no = #{loanNo} LIMIT 1")
    RepaymentPlan selectByLoanNo(@Param("loanNo") String loanNo);

    /**
     * 更新还款计划状态
     * 
     * @param planId 计划ID
     * @param status 状态
     * @return 受影响行数
     */
    @Update("UPDATE repayment_plan SET status = #{status}, update_time = NOW() WHERE id = #{planId}")
    int updateStatus(@Param("planId") Long planId, @Param("status") Integer status);

    /**
     * 更新已还期数和金额
     * 
     * @param planId 计划ID
     * @param paidPeriods 已还期数
     * @param paidAmount 已还金额
     * @param remainingAmount 剩余金额
     * @return 受影响行数
     */
    @Update("UPDATE repayment_plan SET paid_periods = #{paidPeriods}, paid_amount = #{paidAmount}, " +
            "remaining_amount = #{remainingAmount}, update_time = NOW() WHERE id = #{planId}")
    int updatePaidInfo(@Param("planId") Long planId, 
                       @Param("paidPeriods") Integer paidPeriods, 
                       @Param("paidAmount") String paidAmount,
                       @Param("remainingAmount") String remainingAmount);

    /**
     * 获取统计数据
     * 
     * @return 统计数据
     */
    @Select("SELECT " +
            "(SELECT COUNT(1) FROM repayment_plan WHERE DATE(next_repay_date) = CURRENT_DATE()) AS todayDueCount, " +
            "(SELECT SUM(next_repay_amount) FROM repayment_plan WHERE DATE(next_repay_date) = CURRENT_DATE()) AS todayDueAmount, " +
            "(SELECT COUNT(1) FROM repayment_plan WHERE DATE(next_repay_date) = DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY)) AS tomorrowDueCount, " +
            "(SELECT SUM(next_repay_amount) FROM repayment_plan WHERE DATE(next_repay_date) = DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY)) AS tomorrowDueAmount, " +
            "(SELECT COUNT(1) FROM repayment_plan WHERE next_repay_date BETWEEN CURRENT_DATE() AND DATE_ADD(CURRENT_DATE(), INTERVAL 7 DAY)) AS weekDueCount, " +
            "(SELECT SUM(next_repay_amount) FROM repayment_plan WHERE next_repay_date BETWEEN CURRENT_DATE() AND DATE_ADD(CURRENT_DATE(), INTERVAL 7 DAY)) AS weekDueAmount, " +
            "(SELECT COUNT(1) FROM repayment_plan WHERE next_repay_date BETWEEN CURRENT_DATE() AND LAST_DAY(CURRENT_DATE())) AS monthDueCount, " +
            "(SELECT SUM(next_repay_amount) FROM repayment_plan WHERE next_repay_date BETWEEN CURRENT_DATE() AND LAST_DAY(CURRENT_DATE())) AS monthDueAmount, " +
            "(SELECT COUNT(1) FROM repayment_plan WHERE status = 3) AS overdueCount, " +
            "(SELECT SUM(remaining_amount) FROM repayment_plan WHERE status = 3) AS overdueAmount, " +
            "(SELECT COUNT(1) FROM repayment_plan) AS totalCount, " +
            "(SELECT SUM(total_amount) FROM repayment_plan) AS totalAmount, " +
            "(SELECT SUM(paid_amount) FROM repayment_plan) AS paidAmount, " +
            "(SELECT SUM(remaining_amount) FROM repayment_plan) AS remainingAmount")
    Map<String, Object> getStatistics();
} 